﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class FuLiaoXunJiaGuanLi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Visible"] == "false")
                {
                    this.Panel2.Visible = false;
                    this.Button_Save.Visible = false;
                    this.Button_Cancel.Visible = false;
                    this.Button_New.Visible = true;
                }
                else
                {
                    this.Panel2.Visible = true;
                    this.Button_Save.Visible = true;
                    this.Button_Cancel.Visible = true;
                    this.DetailsView1.ChangeMode(DetailsViewMode.Insert);
                }
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {
                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p + 1;
                    }
                }
                else
                {
                    TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
                }
            }

        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
            this.Button_New.Visible = true;
            this.Button_Save.Visible = false;
            this.Button_Cancel.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.SelectCommand = "SELECT * FROM [询价单] where [ID]=" + ((Button)sender).CommandName;
            this.DetailsView1.ChangeMode(DetailsViewMode.Edit);
            this.Panel2.Visible = true;
            this.Button_New.Visible = false;
            this.Button_Save.Visible = true;
            this.Button_Cancel.Visible = true;
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (this.DetailsView1.CurrentMode == DetailsViewMode.Edit)
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
                string 供货单位ID = ((DropDownList)this.DetailsView1.FindControl("GongHuoDanWei")).Text;
                string 辅料ID = ((DropDownList)this.DetailsView1.FindControl("FuLiao")).Text;
                string 单价 = ((TextBox)this.DetailsView1.FindControl("DanJia")).Text;
                if (单价 == "")
                {
                    this.Message.Text = ".单价不能为空值";
                    return;
                }
                if (decimal.Parse(单价) < 0)
                {
                    this.Message.Text = ".单价不能为负数";
                    return;
                }
                string 时间 = ((TextBox)this.DetailsView1.FindControl("RiQi")).Text;
                if (时间 == "")
                {
                    this.Message.Text = ".请选择时间";
                    return;
                }
                string 运费 = ((TextBox)this.DetailsView1.FindControl("YunFei")).Text;

                if (运费 == "")
                {
                    ((TextBox)this.DetailsView1.FindControl("YunFei")).Text = "0";
                }
                else
                {
                    if (decimal.Parse(运费) < 0)
                    {
                        this.Message.Text = ".运费不能为负数";
                        return;
                    }
                }
                string 备注 = ((TextBox)this.DetailsView1.FindControl("BeiZhu")).Text;
                string strSql = @"UPDATE [询价单]SET [供货单位ID]='" + 供货单位ID + "',[货物ID]='" + 辅料ID + "',[单价]='" + 单价 + "',[时间]='" + 时间 + "',[运费]='" + 运费 + "',备注='" + 备注 + "' where ID=" + this.DetailsView1.DataKey.Value;

                SqlCommand cmd = new SqlCommand(strSql, Conn);
                try
                {
                    cmd.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    this.Message.Text = ex.Message;
                }
                finally
                {
                    cmd.Clone();
                    Conn.Close();
                }
                this.Panel2.Visible = false;
                this.SqlDataSource1.SelectCommand = "SELECT * FROM [询价单] where [ID]=" + this.DetailsView1.DataKey.Value;
                this.GridView1.DataBind();
            }
            else if (this.DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                string BiaoShuID = Request.QueryString["BiaoShuID"];
                ((TextBox)this.DetailsView1.FindControl("BiaoShuID")).Text = BiaoShuID;
                SqlDataSource1.InsertCommand = "INSERT INTO [询价单]([标书ID],[供货单位ID],[货物ID],[单价],[时间],[运费],[备注]) VALUES (@标书ID,@供货单位ID,@货物ID,@单价,@时间,@运费,@备注) ";
                SqlDataSource1.InsertParameters.Add("标书ID", BiaoShuID);
                this.DetailsView1.InsertItem(false);
                this.Panel2.Visible = false;
                this.GridView1.DataBind();
            }
            this.Button_New.Visible = true;
            this.Button_Save.Visible = false;
            this.Button_Cancel.Visible = false;
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "Delete FROM [询价单] where [ID]=" + ((Button)sender).CommandName;
            this.SqlDataSource1.Delete();
            this.GridView1.DataBind();
            this.Message.Text = ".删除成功";
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = true;
            this.DetailsView1.ChangeMode(DetailsViewMode.Insert);
            this.Button_New.Visible = false;
            this.Button_Save.Visible = true;
            this.Button_Cancel.Visible = true;
        }
    }
}
